﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MotoGame.Estados.Menu
{
    class Botao
    {
        #region Properties
        private Rectangle shape;
        private String text;
        private Color background;
        private Color foreground;
        private Color highlightedBackground;
        private SpriteFont font;
        private bool highlighted;

        private GraphicsDevice graphicsDevice;
        private Texture2D dummyTexture;

        private Action<Botao> onClick;

        private MouseState last;

        public Rectangle Shape 
        {
            get { return shape; }
            set { shape = value; }
        }

        public String Text
        {
            get { return text; }
            set { text = value; }
        }

        public Color Background
        {
            get { return background; }
            set { background = value; }
        }

        public Color Foreground
        {
            get { return foreground; }
            set { foreground = value; }
        }

        public Color HighlightedBackground
        {
            get { return highlightedBackground; }
            set { highlightedBackground = value; }
        }

        public GraphicsDevice GraphicsDevice
        {
            get { return graphicsDevice; }
            set { graphicsDevice = value; }
        }

        public SpriteFont Font
        {
            get { return font; }
            set { font = value; }
        }

        public Action<Botao> OnClick
        {
            get { return onClick; }
            set { onClick = value; }
        }
        #endregion

        public Botao(GraphicsDevice graphicsDevice, SpriteFont font, Rectangle shape, String text, Action<Botao> onClick)
        {
            GraphicsDevice = graphicsDevice;
            Font = font;
            Shape = shape;
            Background = Color.Yellow;
            Foreground = Color.Red;
            HighlightedBackground = Color.Wheat;
            Text = text;
            OnClick = onClick;

            dummyTexture = new Texture2D(GraphicsDevice, 1, 1);
            dummyTexture.SetData(new Color[] { Color.White });
        }

        #region Operations
        public void Update(GameTime gameTime)
        {
            MouseState current = Mouse.GetState();

            // highlight checking
            highlighted = Shape.Contains(current.X, current.Y);

            // click detection
            if(highlighted // cursor on top 
            && last.LeftButton == ButtonState.Pressed && current.LeftButton == ButtonState.Released // button clicked
            && OnClick != null /* OnClick was defined */)
            {
                OnClick(this);
            }

            last = current;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(dummyTexture, Shape, highlighted ? HighlightedBackground : Background);

            Vector2 textSize = Font.MeasureString(text);
            Vector2 textCenter = new Vector2(Shape.Width / 2, Shape.Height / 2);

            spriteBatch.DrawString(Font, Text, textCenter - (textSize / 2) + new Vector2(Shape.X, Shape.Y), Foreground);
        }
        #endregion
    }
}
